﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ShapeDemo.UC.WinProp;
using System.Windows;
using System.Windows.Input;

namespace ShapeDemo
{
    public class RectDrawer
    {
        private Canvas rectCanvas;
        private List<ASPNETX_WPFRect> lstRect;
        private WinPropRectangle winRectProp;
        private WinPropCommon winCommonProp;

        ASPNETX_WPFRect rectBody;
        TMouse MouseType;

        bool IsRectInDrawing = false;

        public RectDrawer(Canvas canvasContent, List<ASPNETX_WPFRect> lstShape, WinPropRectangle winShapeProp, WinPropCommon winProp)
        {
            rectCanvas = canvasContent;
            lstRect = lstShape;
            winRectProp = winShapeProp;
            winCommonProp = winProp;

            MouseType = TMouse.Drawing;

            rectCanvas.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(rectCanvas_MouseLeftButtonDown);
            rectCanvas.MouseMove += new System.Windows.Input.MouseEventHandler(rectCanvas_MouseMove);
        }

        public void Draw()
        { 
            
        }


        #region 画矩形
        private void rectCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (MouseType == TMouse.Drawing)
            {
                if (IsRectInDrawing == false)
                {
                    rectBody = ResourceCenter.NewRect();
                    rectCanvas.Children.Add(rectBody.RectBody);

                    rectBody.P1 = e.GetPosition(rectCanvas);
                    IsRectInDrawing = true;

                    return;
                }
                if (IsRectInDrawing == true)
                {
                    rectBody.P2 = e.GetPosition(rectCanvas);
                    IsRectInDrawing = false;

                    //添加基本属性
                    rectBody.CommonProperty.Index = winCommonProp.tbIndex.Text;
                    rectBody.CommonProperty.Content = winCommonProp.tbContent.Text;

                    lstRect.Add(rectBody);

                    //BindListBox();

                    return;
                }
            }
        }

        private void rectCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseType == TMouse.Drawing)
            {
                if (IsRectInDrawing == true)
                {
                    Point p = e.GetPosition(rectCanvas);
                    rectBody.P2 = p;

                    rectBody.RectBody.SetValue(Canvas.LeftProperty, rectBody.Left);
                    rectBody.RectBody.SetValue(Canvas.TopProperty, rectBody.Top);
                    rectBody.RectBody.Width = rectBody.Width;
                    rectBody.RectBody.Height = rectBody.Height;

                    winRectProp.X = rectBody.Left;
                    winRectProp.Y = rectBody.Top;

                    winRectProp.Width = rectBody.Width;
                    winRectProp.Height = rectBody.Height;
                }
            }
        }
        #endregion
    }
}
